import { createRouter, createWebHistory, createWebHashHistory } from 'vue-router'
import frontPage from './front'
import companyPage from './company'
import institutionPage from './institution'
import { getToken } from '@/utils/auth'
import { containsIgnoreCase } from '@/utils/ruoyi'

const router = createRouter({
  history: createWebHashHistory('/'),
  routes: [
    {
      path: '/',
      component: () => import('@/views/index.vue'),
      name: 'shouye',
      redirect: '/Home',
      children: frontPage,
    },
    // 匹配不到路由返回404
    {
      path: '/:pathMatch(.*)*',
      component: () => import('@/views/error/404.vue'),
    },

    // 前端后台
    {
      path: '/companyBack',
      name: 'CompanyBack',
      redirect: { name: 'companyIndex' },
      component: () => import('@/views/back/index.vue'),
      children: companyPage,
    },
    // 前端后台
    {
      path: '/institutionBack',
      name: 'InstitutionBack',
      redirect: { name: 'orderSuperMarketPage' },
      component: () => import('@/views/back/index.vue'),
      children: institutionPage,
    },
  ],
})

router.beforeEach((to, from) => {
  if (!containsIgnoreCase(to.path, 'back')) return true

  const token = getToken()

  if (!!token) {
    return true
  }
  return {
    name: 'shouye',
  }
})

export default router
